﻿using ChinaCircleJerk.Data;
using ChinaCircleJerk.Data.Model;
using ChinaCircleJerk.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;

namespace ChinaCircleJerk.Adapter.Data
{
    public class CCJerkDataAdapter : CCJerkAdapter
    {
        public IndexVm GetPosts()
        {
            IndexVm iVm = new IndexVm();
            using (ChinaCircleJerkDbContext db = new ChinaCircleJerkDbContext())
            {
                var results = db.Posts.Where(m => m.IsDeleted == false).Select(a => new PostObjVm
                {
                    Title = a.Title,
                    DateCreated = a.DateCreated,
                    PostId = a.PostId,
                    UserId = a.UserId,
                    User = db.Users.FirstOrDefault(u => u.Id == a.UserId).UserName
                }).ToList();
                iVm.AllPosts = results;
                return iVm;
            }
        }

        public void CreatePost(PostVm pVm)
        {
            using (ChinaCircleJerkDbContext db = new ChinaCircleJerkDbContext())
            {
                db.Posts.Add(new Post() { Title = pVm.Title, Body = pVm.Body, UserId = pVm.UserId, DateCreated = DateTime.Now });
                db.SaveChanges();
            }
        }
        public void CreateReply(ReplyVm rVm)
        {
            using (ChinaCircleJerkDbContext db = new ChinaCircleJerkDbContext())
            {
                db.Replies.Add(new Reply() { Body = rVm.Body, PostId = rVm.PostId, UserId = rVm.UserId });
                db.SaveChanges();
            }
        }
        public EditVm EditPost(int id)
        {
            EditVm eVm = new EditVm();
            using (ChinaCircleJerkDbContext db = new ChinaCircleJerkDbContext())
            {
                eVm.ThisPost = db.Posts.FirstOrDefault(m => m.PostId == id);
            }
            return eVm;
        }
        public void UpdatePost(EditVm eVm)
        {
            using (ChinaCircleJerkDbContext db = new ChinaCircleJerkDbContext())
            {
                var post = db.Posts.FirstOrDefault(m => m.PostId == eVm.ThisPost.PostId);
                post.Title = eVm.ThisPost.Title;
                post.Body = eVm.ThisPost.Body;
                post.DateEdited = DateTime.Now;
                db.SaveChanges();
            }
        }
        public EditVm EditReply(int id)
        {
            EditVm eVm = new EditVm();
            using (ChinaCircleJerkDbContext db = new ChinaCircleJerkDbContext())
            {
                eVm.ThisReply = db.Replies.FirstOrDefault(m => m.ReplyId == id);
            }
            return eVm;
        }
        public void UpdateReply(EditVm eVm)
        {
            using (ChinaCircleJerkDbContext db = new ChinaCircleJerkDbContext())
            {
                var reply = db.Replies.FirstOrDefault(m => m.ReplyId == eVm.ThisReply.ReplyId);
                reply.Body = eVm.ThisReply.Body;
                reply.DateEdited = DateTime.Now;
                db.SaveChanges();
            }
        }
        public DetailsVm PostDetails(int id)
        {
            DetailsVm dVm = new DetailsVm();
            using (ChinaCircleJerkDbContext db = new ChinaCircleJerkDbContext())
            {
                dVm.ThatPost = db.Posts.FirstOrDefault(m => m.PostId == id);
                var allReplies = db.Replies.Where(m => m.PostId == id && m.IsDeleted == false).Select(a => new PostObjVm 
                { 
                    Body = a.Body,
                    PostId = a.PostId,
                    ReplyId = a.ReplyId,
                    UserId = a.UserId,
                    User = db.Users.FirstOrDefault(u => u.Id == a.UserId).UserName
                }).ToList();
                dVm.AllReplies = allReplies;
            }
            return dVm;
        }
        public void DeletePost(int id)
        {
            using (ChinaCircleJerkDbContext db = new ChinaCircleJerkDbContext())
            {
                var post = db.Posts.FirstOrDefault(m => m.PostId == id);
                post.IsDeleted = true;
                db.SaveChanges();
            }
        }
        public void DeleteReply(int id)
        {
            using (ChinaCircleJerkDbContext db = new ChinaCircleJerkDbContext())
            {
                var reply = db.Replies.FirstOrDefault(m => m.ReplyId == id);
                reply.IsDeleted = true;
                db.SaveChanges();
            }
        }
        public void UpdateUserRole(UsersVm uVm)
        {
            using (ChinaCircleJerkDbContext db = new ChinaCircleJerkDbContext())
            {
                var user = db.Users.FirstOrDefault(m => m.Id == uVm.User.Id);
                Roles.AddUserToRole(uVm.User.UserName, "Moderator");
            }        
        }
        public void RemoveUserRole(UsersVm uVm)
        {
            using (ChinaCircleJerkDbContext db = new ChinaCircleJerkDbContext())
            {
                var user = db.Users.FirstOrDefault(m => m.Id == uVm.User.Id);
                Roles.RemoveUserFromRole(uVm.User.UserName, "Moderator");
            }
        }
    }
}